package com.ruoyi.web.controller.parties;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.PermitReportFile;
import com.ruoyi.system.service.PermitReportFileService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 许可证件和报告类型分类管理(PermitReportFile)表控制层
 *
 * @author makejava
 * @since 2022-11-18 09:34:20
 */
@RestController
@RequestMapping("permit/report/file")
public class PermitReportFileController {


    /**
     * 服务对象
     */
    @Resource
    private PermitReportFileService permitReportFileService;

    /**
     * 分页查询
     *
     * @param permitReportFile 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public TableDataInfo queryByPage(PermitReportFile permitReportFile) {
        //校验查询类型是否为空;
        if (null == permitReportFile || null == permitReportFile.getType()) {
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.BAD_REQUEST);
            rspData.setMsg("查询类型不可为空:[type:1:许可证 2:报告]");
            //rspData.setRows(new ArrayList<>());
            //rspData.setTotal(0);
            return rspData;
        }
        startPage();
        List<PermitReportFile> list = permitReportFileService.selectList(permitReportFile);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<PermitReportFile> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.permitReportFileService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param permitReportFile 实体
     * @return 新增结果
     */
    @PreAuthorize("@ss.hasPermi('permit:report:add')")
    @PostMapping("add")
    public ResponseEntity<PermitReportFile> add(@Validated @RequestBody PermitReportFile permitReportFile) {
        return ResponseEntity.ok(this.permitReportFileService.insert(permitReportFile));
    }

    /**
     * 编辑数据
     *
     * @param permitReportFile 实体
     * @return 编辑结果
     */
    @PreAuthorize("@ss.hasPermi('permit:report:update')")
    @PutMapping("edit")
    public ResponseEntity<PermitReportFile> edit(@Validated @RequestBody PermitReportFile permitReportFile) {
        return ResponseEntity.ok(this.permitReportFileService.update(permitReportFile));
    }

    /**
     * 删除数据
     *
     * @param permitReportFile 主键
     * @return 删除是否成功
     */
    @PreAuthorize("@ss.hasPermi('permit:report:delete')")
    @DeleteMapping("delete")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody PermitReportFile permitReportFile) {
        return ResponseEntity.ok(this.permitReportFileService.deleteById(permitReportFile.getId()));
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageUtils.startPage();
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

}

